home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2000 January / Macworld (2000-01).dmg / Shareware World / Info / For Developers / OSA Script.sea / OSA Script / ExecuteEventInScript.p < prev    next >
Text File  |  1999-09-27  |  4KB  |  90 lines

  1. unit ExecuteEventInScript;
  2. {©23/7/99 — Jan Skarbek. http://www.ozemail.com.au/~pbird. pbird@ozemail.com.au}
  3. {You are welcome to distribute this AppleEvent handling sample freely, but must keep the package intact.}
  4.  
  5. {Comments, questions, and suggestions invited.}
  6.  
  7. {Formatting based on Ingemar Ragnemalm's pi_script.p... which is based... :) }
  8.  
  9. {See ExecuteScript.p for more details}
  10.  
  11. interface
  12.     uses
  13.         Processes, Resources, AppleEvents, Components, OSA;
  14.  
  15.     function ExecuteEventInCompiledScriptResource (scriptResID: Integer;
  16.                                     sentData: AEDesc;
  17.                                     var replyData: AEDesc;
  18.                                     class, ID: DescType): OSErr;
  19.  
  20. implementation
  21.  
  22.     function ExecuteEventInCompiledScriptResource (scriptResID: Integer;
  23.                                     sentData: AEDesc;
  24.                                     var replyData: AEDesc;
  25.                                     class, ID: DescType): OSErr;
  26.         var
  27.             iErr, ie: OSErr;
  28.             ignoreErr: OSAError;
  29.             scriptData: Handle;
  30.             ae, reply: AppleEvent;
  31.             scriptDesc, addressDesc: AEDesc;
  32.             scriptID: OSAID;
  33.             myScriptingComponent: ComponentInstance;
  34.             myPSN: ProcessSerialNumber;
  35.     begin
  36.         ae.dataHandle := nil;
  37.         reply.dataHandle := nil;
  38.         addressDesc.dataHandle := nil;
  39. {get script from resource}
  40.         scriptData := GetResource('scpt', scriptResID);
  41.         iErr := ResError;
  42.         if (iErr = noErr) and (scriptData = nil) then
  43.             iErr := ResNotFound;
  44.         if iErr = noErr then
  45.             begin
  46. {load the script data}
  47.                 scriptDesc.descriptorType := typeOSAGenericStorage;
  48.                 scriptDesc.dataHandle := scriptData;
  49. {open a connection with AppleScript component}
  50. {NOTE that is you do this often, you should use a global instead of a local for speed}
  51.                 myScriptingComponent := OpenDefaultComponent(kOSAComponentType, kOSAGenericscriptingComponentSubtype);
  52. {load script… create a context}
  53.                 iErr := OSALoad(myScriptingComponent, scriptDesc, kOSAModeNull, scriptID);
  54. {create an address descriptor describing the current application}
  55.                 if iErr = noErr then
  56.                     begin
  57.                         myPSN.highLongOfPSN := 0;
  58.                         myPSN.lowLongOfPSN := kCurrentProcess;
  59.                         iErr := AECreateDesc(typeProcessSerialNumber, @myPSN, SizeOf(myPSN), addressDesc);
  60.                     end;
  61. {create AEs for the sent and reply data, with the Class and ID of the handler within the script}
  62. {NOTE that the reply event must be pre-built, or there is nowhere for the reply data to go. It doesn't much matter what's}
  63. {inside it, as long as it's valid (has a memory allocation for the dataHandle)}
  64.                 if iErr = noErr then
  65.                     iErr := AECreateAppleEvent(class, ID, addressDesc, kAutoGenerateReturnID, kAnyTransactionID, ae);
  66.                 if iErr = noErr then
  67.                     iErr := AECreateAppleEvent(class, ID, addressDesc, kAutoGenerateReturnID, kAnyTransactionID, reply);
  68. {add the parameter data for the script handler. Use the direct parameter '----'}
  69.                 if iErr = noErr then
  70.                     iErr := AEPutKeyDesc(ae, keyDirectObject, sentData);
  71. {execute the script handler within the script by sending the script context (AKA Script Object) an event}
  72.                 if iErr = noErr then
  73.                     iErr := OSADoEvent(myScriptingComponent, ae, scriptID, kOSAModeNull, reply);
  74. {extract the data from the reply descriptor}
  75.                 if iErr = noErr then
  76.                     iErr := AEGetKeyDesc(reply, keyDirectObject, typeWildCard, replyData);
  77. {NOTE: clean up, but DON'T dispose of scriptDesc, as it' s really a resource handle, and will be disposed of as such, later}
  78.                 ie := AEDisposeDesc(addressDesc);
  79.                 ie := AEDisposeDesc(ae);
  80.                 ie := AEDisposeDesc(reply);
  81.                 ignoreErr := OSADispose(myScriptingComponent, scriptID);
  82.             end;
  83. {dispose the memory used by the script resource}
  84.         ReleaseResource(scriptData);
  85. {close the connection with the AppleScript component}
  86.         ignoreErr := CloseComponent(myScriptingComponent);
  87.         ExecuteEventInCompiledScriptResource := iErr;
  88.     end;
  89.  
  90. end.